<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 签到活动ajax类
 * @package  	ajax
 * @subpackage  backend
 * @create by allen.qiang
 */

quanli_require('module/base/promotion/signIn.class.php');

class ajax_signedTo {

    public function execute(){

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //签到活动列表
                    $this->searchSignInList();
                    break;
                case 2: //保存签到活动
                    $this->edit();
                    break;
                case 3: //签到活动使用禁用
                    $this->useAction();
                    break;
                case 4: //删除签到活动
                    $this->deleteSignIn();
                    break;
                case 6: //查询单个签到活动
                    $this->searchSignIn();
                    break;
                default:
                    $this->toReturn('参数错误');
                    break;

            }

        } else {

            $this->toReturn('参数错误');

        }

    }

    /**
     * 返回信息
     * @param string $message
     * @param int $code
     * @param mixed $data
     */
    private function toReturn( $message, $code = 400, $data = '' ) {

        die( json_encode( array( 'code' => $code, 'message' => $message, 'data' => $data ) ) );

    }

    private function searchSignIn() {

        global $G_PGLOB;

        if (!isset($_REQUEST['id']) || !is_numeric($_REQUEST['id']) || $_REQUEST['id'] <= 0) {
            $this->toReturn('参数错误');
        }

        //实例签到类
        $signIn = new signIn($G_PGLOB->DBCA);

        //查询签到信息
        $arg_array = $_REQUEST;
        $sign_in_array = $signIn->searchSigneToList( $arg_array );
        if (!$sign_in_array) {
            $this->toReturn('签到活动没有找到', 404);
        } else {
            $this->toReturn('ok', 200, $sign_in_array[0]);
        }

    }

    //签到活动列表
    private function searchSignInList(){

        global $G_PGLOB;

        $promotionHandler = new signIn($G_PGLOB->DBCA);

        $arg_array = $_REQUEST;
        if (isset($_REQUEST['name']))
            $arg_array['promotionName'] = $_REQUEST['name'];
        if (isset($_REQUEST['start_Date']))
            $arg_array['startDate'] = $_REQUEST['start_Date'];
        if (isset($_REQUEST['end_Date']))
            $arg_array['endDate'] = $_REQUEST['end_Date'];

        $result_data = $promotionHandler->searchSigneToList( $arg_array );

        $result_array = array(
            'page'  => $arg_array['page'] ,
            'total' => $arg_array['count'] ,
            'rows'  => array()
        );

        if ($result_data) {

            $edit_permission = $G_PGLOB->has_permission(4527);

            foreach($result_data as $val) {

                $extra = '无';
                if ($val['continuousDay'] > 0) {
                    $extra = '连续签到' . $val['continuousDay'] . '天，额外赠送' . $val['extraPoints'] . '积分';
                }

                if($edit_permission){

                    $result_array['rows'][] = array(
                        'id' 	=> $val['promotionID'],
                        'cell' 	=> array(
                            '<a  href="javascript: void(0);" onclick="signed.edit.search(' . $val['promotionID'] . ')">修改</a>',
                            $val['promotionName'],
                            $val['startDate'],
                            $val['endDate'],
                            $val['points'],
                            $extra,
                            $val['useFlg'] == 1 ?
                                '<a href="javascript: void(0);" onclick=signed.use.check(\'' . $val['promotionID'] . '\',0);>启用</a>':
                                '<a href="javascript: void(0);" onclick=signed.use.check(\'' . $val['promotionID'] . '\',1); >禁用</a>',
                            $val['description']
                        )
                    );

                }else{

                    $result_array['rows'][] = array(
                        'id' 	=> $val['pcustID'],
                        'cell' 	=> array(
                            $val['promotionName'],
                            $val['startDate'],
                            $val['endDate'],
                            $val['points'],
                            $extra,
                            $val['useFlgName'],
                            $val['description']
                        ),
                    );

                }
            }
        }

        echo json_encode( $result_array );


    }

    //保存签到活动
    private function edit(){

        global $G_PGLOB;

        $promotionHandler = new signIn( $G_PGLOB->DBCA );

        $_REQUEST['operator'] = $G_PGLOB->currentUserID;
        $result = $promotionHandler->editSignIn( $_REQUEST );

        echo json_encode($result);

    }

    //签到活动使用禁用
    private function useAction(){

        global $G_PGLOB;

        $signIn = new signIn($G_PGLOB->DBCA);

        $arg_array = $_REQUEST;

        $result_array = $signIn->useSigned( $arg_array );

        echo json_encode( $result_array );

    }

    //删除签到活动
    private function deleteSignIn(){

        global $G_PGLOB;

        $signIn = new signIn($G_PGLOB->DBCA);

        $arg_array = $_REQUEST;

        $result_array = $signIn->deleteSignIn($arg_array);

        echo json_encode($result_array);

    }

}
